/**
 * 
 */
package com.demo2do.strawberry.entity.startup;

import org.apache.commons.lang.ArrayUtils;

/**
 * Describe the status of a startup
 * 
 * @author Downpour
 */
public enum Status {

	/** startups are editing once created */
	EDITING("enum.startup-status.editing"),

	/** startups are sending to admin for auditing */
	AUDITING("enum.startup-status.auditing"),
	
	/** audit fail */
	AUDIT_FAIL("enum.startup-status.audit-fail"),
	
	/** audit successfully */
	OPERATING("enum.startup-status.operating"),
	
	/** startups end fund according to date */
	COMPLETE("enum.startup-status.complete"),
	
	/** startups are editable when they are editing and audit fail */
	NEW("enum.startup-status.new", EDITING, AUDIT_FAIL);
	
	private String key;
	
	private boolean virtual;
	
	private Status[] linkedStatus;
	
	/**
	 * 
	 */
	private Status() {
	
	}
	
	/**
	 * 
	 * @param key
	 * @param linkedStatus
	 */
	private Status(String key, Status ... linkedStatus) {
		this.key = key;
		this.virtual = (linkedStatus != null);
		this.linkedStatus = linkedStatus;
	}
	
	/**
	 * @return the key
	 */
	public String getKey() {
		return key;
	}
	
	/**
	 * @return the alias
	 */
	public String getAlias() {
		return this.key.substring(key.lastIndexOf(".") + 1);
	}
	
	/**
	 * @return the virtual
	 */
	public boolean isVirtual() {
		return virtual;
	}
	
	/**
	 * @return the linkedStatus
	 */
	public Status[] getLinkedStatus() {
		return linkedStatus;
	}
	
	/**
	 * return actual status when retrieve startup according to status
	 * 
	 * @return 
	 */
	public Status[] getActualStatus() {
		if(ArrayUtils.isEmpty(linkedStatus)) {
			return new Status[]{this};
		}
		return linkedStatus;
	}
	
	/**
	 * determine whether startup is on operating
	 * 
	 * @return
	 */
	public boolean isOperating() {
		if(this.getAlias().equalsIgnoreCase("operating")) {
			return true;
		}
		return false;
	}
	
	/**
	 * @param value
	 * @return
	 */
	public static Status aliasOf(String value) {
		for(Status status : Status.values()) {
			if(status.getAlias().equalsIgnoreCase(value)) {
				return status;
			}
		}
		return null;
	}
	
}
